/**
 * Copyright (c) 2011-2015, Mobangjack 莫帮杰 (mobangjack@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.twogen.im;

import java.awt.image.ColorModel;
import java.util.Arrays;

/**
 * ImageFilter：有用的滤镜
 * @author 帮杰
 *
 */
public class ImageFilter {

	/**Class: ImageFilter
	 * Method: median
	 * Function: 中值滤波
	 * @param pixelImage 以像素表示的图像
	 * @return void
	 */
	public void median(PixelImage pixelImage) {
		int k[][] = new int[3][3];
		int f[] = new int[3];
		ColorModel cm = ColorModel.getRGBdefault();
		int ih = pixelImage.getHeight();
		int iw = pixelImage.getWidth();
		int pixels[] = pixelImage.getPixels();
		for (int i = 1; i < ih - 1; i++) {
			for (int j = 1; j < iw - 1; j++) {
				int alpha = cm.getAlpha(pixels[i * iw + j]);

				k[0][0] = cm.getRed(pixels[(i-1) * iw + j - 1]);
				k[0][1] = cm.getRed(pixels[(i-1) * iw + j]);
				k[0][2] = cm.getRed(pixels[(i-1) * iw + j + 1]);
				k[1][0] = cm.getRed(pixels[i * iw + j - 1]);
				k[1][1] = cm.getRed(pixels[i * iw + j]);
				k[1][2] = cm.getRed(pixels[i * iw + j + 1]);
				k[2][0] = cm.getRed(pixels[(i+1) * iw + j - 1]);
				k[2][1] = cm.getRed(pixels[(i+1) * iw + j]);
				k[2][2] = cm.getRed(pixels[(i+1) * iw + j + 1]);
				Arrays.sort(k[0]);
				Arrays.sort(k[1]);
				Arrays.sort(k[2]);
				f[0] = k[0][0];
				f[1] = k[1][1];
				f[2] = k[2][2];
				Arrays.sort(f);
				int red = f[1];
				
				k[0][0] = cm.getGreen(pixels[(i-1) * iw + j - 1]);
				k[0][1] = cm.getGreen(pixels[(i-1) * iw + j]);
				k[0][2] = cm.getGreen(pixels[(i-1) * iw + j + 1]);
				k[1][0] = cm.getGreen(pixels[i * iw + j - 1]);
				k[1][1] = cm.getGreen(pixels[i * iw + j]);
				k[1][2] = cm.getGreen(pixels[i * iw + j + 1]);
				k[2][0] = cm.getGreen(pixels[(i+1) * iw + j - 1]);
				k[2][1] = cm.getGreen(pixels[(i+1) * iw + j]);
				k[2][2] = cm.getGreen(pixels[(i+1) * iw + j + 1]);
				Arrays.sort(k[0]);
				Arrays.sort(k[1]);
				Arrays.sort(k[2]);
				f[0] = k[0][0];
				f[1] = k[1][1];
				f[2] = k[2][2];
				Arrays.sort(f);
				int green = f[1];
				
				k[0][0] = cm.getBlue(pixels[(i-1) * iw + j - 1]);
				k[0][1] = cm.getBlue(pixels[(i-1) * iw + j]);
				k[0][2] = cm.getBlue(pixels[(i-1) * iw + j + 1]);
				k[1][0] = cm.getBlue(pixels[i * iw + j - 1]);
				k[1][1] = cm.getBlue(pixels[i * iw + j]);
				k[1][2] = cm.getBlue(pixels[i * iw + j + 1]);
				k[2][0] = cm.getBlue(pixels[(i+1) * iw + j - 1]);
				k[2][1] = cm.getBlue(pixels[(i+1) * iw + j]);
				k[2][2] = cm.getBlue(pixels[(i+1) * iw + j + 1]);
				Arrays.sort(k[0]);
				Arrays.sort(k[1]);
				Arrays.sort(k[2]);
				f[0] = k[0][0];
				f[1] = k[1][1];
				f[2] = k[2][2];
				Arrays.sort(f);
				int blue = f[1];
				
				pixels[i * iw + j] = alpha << 24 | red << 16 | green << 8 | blue;
			}
		}
	}
	
}
